package java44th.group.controller.wx.comment;

import java44th.group.bean.admin.vo.BaseRespVo;
import java44th.group.bean.common.CommonData;
import java44th.group.bean.po.MarketUser;
import java44th.group.bean.wx.bo.commont.PostBo;
import java44th.group.bean.wx.bo.commont.ListBo;
import java44th.group.bean.wx.vo.comment.CountVo;
import java44th.group.bean.wx.vo.comment.ListVo;
import java44th.group.bean.wx.vo.comment.PostVo;
import java44th.group.service.wx.comment.WxCommentService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ProjectName project-second
 * @Descriptiom TODO
 * @Aurhor: 蔡文强
 * @Since: 2022/10/15 15:43
 */
@RestController
@RequestMapping("wx/comment")
public class WxCommentController {

    @Autowired
    WxCommentService wxCommentService;


    @RequestMapping(value = "list", method = RequestMethod.GET)
    public BaseRespVo list(ListBo listBo) {

        CommonData<ListVo> listVoCommonData = wxCommentService.queryCommentList(listBo);

        return BaseRespVo.ok(listVoCommonData);
    }

    // http://182.92.235.201:8083/wx/comment/count?valueId=405&type=1
    @RequestMapping(value = "count",method = RequestMethod.GET)
    public BaseRespVo count(Integer valueId,Integer type){

        CountVo countVo = wxCommentService.queryCommentCount(valueId,type);

        return BaseRespVo.ok(countVo);
    }

    @Transactional
    @RequestMapping(value = "post",method = RequestMethod.POST)
    public BaseRespVo post(@RequestBody PostBo postBo){

        // 获得用户id
        Subject subject = SecurityUtils.getSubject();
        MarketUser marketUser = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
        if(marketUser == null){
            return BaseRespVo.unAuthc();
        }
        Integer userId = marketUser.getId();


        // 插入数据并返回相关信息
        PostVo postVo = wxCommentService.insertCommentAndReturnInfo(postBo,userId);

        return BaseRespVo.ok(postVo);
    }
}
